﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LiveNet.Contacts
{
    public class Email : DictionaryBase
    {
        #region Private Data
        private bool m_changed = false;
        #endregion

        #region Constructors
        internal Email()
        {
            BuildDictionary();
        }

        internal Email(XmlNode xmlnode)
        {
            BuildDictionary();
            FromXml(xmlnode);
        }

        private void BuildDictionary()
        {
            this.Add("ID", "");
            this.Add("EmailType", "");
            this.Add("Address", "");
            this.Add("IsIMEnabled", "");
            this.Add("IsDefault", "");
        }
        #endregion

        #region Public Properties
        public int ID
        {
            get { return Convert.ToInt32(this["ID"]); }
            set { this["ID"] = Values.ToString(); }
        }
        
        public EmailAddressType EmailType
        {
            get { return (EmailAddressType)Enum.Parse(Type.GetType("LiveNet.Contacts.EmailAddressType"), this["EmailType"]); }
            set { this["EmailType"] = value.ToString(); }
        }
        
        public string Address
        {
            get { return this["Address"]; }
            set { this["Address"] = value; }
        }

        public bool IsIMEnabled
        {
            get { return Convert.ToBoolean(this["IsIMEnabled"]); }
        }

        public bool IsDefault
        {
            get { return Convert.ToBoolean(this["IsDefault"]); }
            set { this["IsDefault"] = value.ToString(); }
        }
        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string xml = "<Email>";
            foreach (DictionaryEntry de in this)
            {
                if (op == OperationType.Update && (de.Key.ToString() == "ID" || de.Key.ToString() == "IsIMEnabled" || de.Key == "EmailType")) continue;
                if (op == OperationType.Create && (de.Key.ToString() == "ID" || de.Key.ToString() == "IsIMEnabled")) continue;
                xml = xml + "<" + de.Key + ">" + de.Value + "</" + de.Key + ">";
            }
            xml = xml + "</Email>";
            xmlDoc.LoadXml(xml);
            return xmlDoc;
        }
        #endregion

        #region Internal Methods
        public void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNode node = xmlnode.SelectSingleNode("ID");
            if (node != null) this["ID"] = node.InnerText;
            node = xmlnode.SelectSingleNode("EmailType");
            if (node != null) this["EmailType"] = node.InnerText;
            node = xmlnode.SelectSingleNode("Address");
            if (node != null) this["Address"] = node.InnerText;
            node = xmlnode.SelectSingleNode("IsIMEnabled");
            if (node != null) this["IsIMEnabled"] = node.InnerText;
            node = xmlnode.SelectSingleNode("IsDefault");
            if (node != null) this["IsDefault"] = node.InnerText;
        }
        #endregion

        #region Dictionary Methods
        public String this[String key]
        {
            get { return ((String)Dictionary[key]); }
            set { Dictionary[key] = value; }
        }

        public ICollection Keys
        {
            get { return (Dictionary.Keys); }
        }

        public ICollection Values
        {
            get { return (Dictionary.Values); }
        }

        public void Add(String key, String value)
        {
            Dictionary.Add(key, value);
        }

        public bool Contains(String key)
        {
            return (Dictionary.Contains(key));
        }

        public void Remove(String key)
        {
            Dictionary.Remove(key);
        }

        protected override void OnInsert(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }

        protected override void OnRemove(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");
        }

        protected override void OnSet(Object key, Object oldValue, Object newValue)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (newValue.GetType() != typeof(System.String))
                throw new ArgumentException("newValue must be of type String.", "newValue");
        }

        protected override void OnValidate(Object key, Object value)
        {
            if (key.GetType() != typeof(System.String))
                throw new ArgumentException("key must be of type String.", "key");

            if (value.GetType() != typeof(System.String))
                throw new ArgumentException("value must be of type String.", "value");
        }
        #endregion
    }
}
